package servlets;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domein.Bestelling;
import domein.Bestellingen;
import domein.BrandstofBestelling;

/**
 * Servlet implementation class Testservlet
 */
@WebServlet(name="BrandstofBestellen", urlPatterns="/medewerker/BestelBrandstof.do")
public class BrandstofBestellen extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BrandstofBestellen() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int aantal = 0;
		if (request.getParameter("aantalValue") == null || request.getParameter("aantalValue").isEmpty()) {
			response.sendRedirect("brandstof-bestellen.jsp?error=Geen+aantal+ingevuld");
			return;
		}
		else if(request.getParameter("brandstof") == null){
			response.sendRedirect("brandstof-bestellen.jsp?error=Geen+brandstof+geselecteerd");
			return;
		}
		else{
		BrandstofBestelling bb = new BrandstofBestelling();
		try{ 
			aantal = Integer.parseInt(request.getParameter("aantalValue"));
		}
		catch(NumberFormatException nfe){
			response.sendRedirect("brandstof-bestellen.jsp?error=Geen+aantal+ingevuld");
			return;
		}
		bb.generateBestelNr();
		bb.setAantal(aantal);
		bb.setBrandstof(request.getParameter("brandstof"));
		Bestellingen.addBestelling((Bestelling)bb);
		System.out.println(bb.getBestelNummer());
		response.sendRedirect("brandstof-bestellen.jsp?error=Brandstof+besteld");
	}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
